$(function(){
	var $pagebar=$("#pagebar ul"),$pageInfo=$("#page_info"),
	pageNo=1,pageSize=10,count=0; type = 0,num=0,joinTable=$("[name= joinTable]").val();//初始值
	number = "";
	/**---------------封装分页方法,count:总条目数,pageNo:当前页---------------*/
	function pageit(count){
		$pagebar.empty();
	//	$('input[type=number]').val(0);//初始化
		//首页和上一页
		if(pageNo==1){
			$pagebar.append("<li id='start_page' class='disabled'><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page'  class='disabled'><a href='javascript:void(0);'>上一页</a></li>");
		}else{
			$pagebar.append("<li id='start_page' pn=1><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page' pn="+(pageNo-1)+"><a href='javascript:void(0);'>上一页</a></li>");
		}
		var tmp=parseInt(count/10);
		var page_size=count%10==0?(tmp):(tmp+1);//总页数
		//中间数字页
		if(page_size<=5){
			//如果小于5页,则全部显示
			for(var i = 0; i <page_size; i++) {
				if(i==pageNo-1){
					$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}else{
					$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}
			}
		}else{
			//如果总页数大于5页,显示当前页和前后各两页以及两个...符号
			if(pageNo<3){
				for(var i = 0; i <5; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
			}else if(pageNo>page_size-2){
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
				for(var i = page_size-5; i <page_size; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}else{
				for (var i = pageNo-3; i <pageNo+2; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(pageNo)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}
		}
		//下一页和末页
		if(page_size==0||pageNo==page_size){
			$pagebar.append("<li id='next_page' class='disabled'><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' class='disabled'><a href='javascript:void(0);'>末页</a></li>");
			$pagebar.append("<li class='disabled'> &nbsp;转到&nbsp;<input  type='number' min='0' max='"+page_size+"' style='width:70px;height:33px;clear:both;border:1px solid #eee;'>&nbsp;页</li>");
		}else{
			$pagebar.append("<li id='next_page' pn="+(pageNo+1)+"><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' pn="+page_size+"><a href='javascript:void(0);'>末页</a></li>");
			$pagebar.append("<li class='disabled'>&nbsp;转到&nbsp;<input  type='number' min='0' max='"+page_size+"' style='width:70px;height:33px;clear:both;border:1px solid #eee;'>&nbsp;页</li>");
		}
		
		//绑定事件
		$pagebar.find("li").not(".disabled").on("click",function(){
				pageNo=parseInt($(this).attr("pn"));//更新当前页码
				reloadList();
				$('#pagebar input[type=number]').val();
		});
		//分页信息条
		$pageInfo.find("span").each(function(i,k){
			if(i==0){
				if(page_size==0){
					$(k).html(0);
				}else{
					$(k).html((pageNo-1)*10+1);
				}
			}else if(i==1){
				$(k).html(count>pageNo*10?pageNo*10:count);
			}else{
				$(k).html(count);
			}
		});
	//页面跳转
	//$('input[type=number]').blur(function () {  
	//	goto($(this).val());
	//});  
	$('#pagebar input[type=number]').focus(function () {  
		$(document).keypress(function(event){
			if(event.keyCode == 13) {
				gotoPage($('#pagebar input[type=number]').val());
			}
		});
	});  
	
	//$('input[type=number]').val(number);
	function gotoPage(pagenumber){
		if(pagenumber>=0){
		    if(pagenumber>=page_size){
				pageNo=parseInt(page_size);// 更新当前页码为总页数
		    }else{
				pageNo=parseInt(pagenumber);// 更新当前页码
		    }
		    number = pageNo;
			reloadList();
	    }else{
	    	alert("请输入合法的页码");
	    }
	}
	return number;
	}
	/**------------------加载列表---------------------*/
	var noRecord= "<font style='color:red;font-weight:bold;font-size:13px;'>无</font>";
	var params={"pageNo":1,"pageSize":pageSize,"type":type,"num":num,"joinTable":joinTable};
	var authStatus = {false:"未认证",true:"已认证"};
	var delStatus = {false:"未删除 ",true:" 删除"};
	function reloadList(){
		params.pageNo=pageNo;
		params.pageSize=pageSize;
		params.type = type;
		params.num  = num;
		params.joinTable  = joinTable;
		$.get(serverURL+"file/getFileListByCondition",params,function(data){
			try {
				if(data.code==0){
					data=data.data;
					$("#same_dict_table").find("tbody").empty();
					count=data.pagination.totalCount;
					if(count>0){
						var html="";
						$.each(data.list,function(i,k){
							html+="<tr>" +
							"<td id='pkc'><input type='checkbox' id='"+k.fileId+"' class='checkbox' col='"+i+"' ></td>" +
							"<td><a href=http://ow365.cn/?i=11328&furl=http://www.gongchengjie.cn//"+k.path+" target='_blank'>"+(k.name?k.name:noRecord)+"</a></td>"+ 
							"<td>"+k.introduction+"</td>"+ 
							"<td>"+k.downloadCount+"</td>"+ 
							"<td>"+k.collectionCount+"</td>"+ 
							"<td>"+k.commentCount+"</td>"+ 
							"</tr>";
						});
						$("#same_dict_table").find("tbody").append(html);
					}else{
						$("#same_dict_table").find("tbody").append("<tr><td colspan="+$("thead th").length+">无数据</td></tr>");
					}
					var pageNum =  pageit(count);//重绘工具栏
					$('#pagebar input[type=number]').val(pageNum);
					$('#num').val(num);
				}else if(data.code!=-1){
					alert("数据加载出错，请稍后重试");
				}
			} catch (e) {
				alert("数据加载出错，请稍后重试");
			}
		});
	}
	reloadList();
	//全选与取消全选
	$("#pkall").change(function(){
		if($(this).prop("checked")){
			$("#same_dict_table").find("#pkc :checkbox").prop("checked",true);
		}else{
			$("#same_dict_table").find("#pkc :checkbox").prop("checked",false);
		}
	});
	//获取选中的行
	function getSelectedCols(){
		return $("#same_dict_table").find("#pkc :checkbox").filter(":checked");
	}

	$(".dropdown-menu li a").click(function() {
		text = $(this).text();
		condition = $(this).attr("id") ; 
		$("#num").val("");
		$.get(serverURL+"file/getCountByCondition",{"type" : $(this).attr("id"),"joinTable" : joinTable},function(data){
			try {
				if(data.code==0){
						$("#btnGroupDrop1").html(text+"<span class='caret'></span>");
						$("#num").attr("placeholder",text+"最大值为"+data.data);
						$("#btnGroupDrop1").attr("condition",condition);
					}else{
						$("#btnGroupDrop1").html(text+"<span class='caret'></span>");
						$("#num").attr("placeholder",text+"最大值为0");
						$("#btnGroupDrop1").attr("condition",condition);
					}
			} catch (e) {
				alert("数据加载出错，请稍后重试");
			}
		});
	});
	
	
	//筛选
	$("#search").click(function(){
		 type = $("#btnGroupDrop1").attr("condition");
		 if(type == null){
			  alert("请选筛选条件");
			  return false;
		 }
		 num = $("#num").val();
		 reloadList();
	});
//	var $formUpload=$("#form_upload");
    /** ************增加文件*************** */
		var $formAdd=$("#form_add");
    	$("select[name=top]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=second]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=detail]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=content]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	$("select[name=project_mode]").empty().html("<option value='0' selected='selected'>请选择...</option>");
    	addCategory($formAdd);
    	addPlan();
    	addGrade();
    
	function checkAddForm(){
		if($("[name=top]").val() == 0){
			warnInput($("[name=top]"));
			return false;
		}
		if($("[name=second]").val() == 0){
			warnInput($("[name=second]"));
			return false;
		}
//		if($("[name=detail]").val() == 0){
//			warnInput($("[name=detail]"));
//			return false;
//		}
		if($("[name=hierId]").val() == 0){
			warnInput($("[name=hierId]"));
			return false;
		}
		return true;
	}
	
	 $("#btn_confirm_add").click(function() {
		if (checkAddForm()) {
			tcId =$formAdd.find($("[name=top]")).val();
			scId =$formAdd.find($("[name=second]")).val();
			hierId = $formAdd.find($("[name=hierId]")).val();
			grade = $formAdd.find($("[name=grade]")).val();
			
			 var $curCol = $("#same_dict_table").find("#pkc :checkbox").filter(":checked");
			if ($curCol.length == 0) {
				alert("请至少选择一条引用记录");
				return false;
			}
			var fileIds = "";
			$.each($curCol, function(i, k) {
				fileIds += $curCol[i].id + ",";
			})
			fileIds = fileIds.substring(0, fileIds.length - 1);	 
			
			dcIds = $formAdd.find($("[name=detail]")).val();
			
			if($formAdd.find($("[name=content]")).val() != 0){
				dcIds += ","+$formAdd.find($("[name=content]")).val() ;
			}
			
			if($formAdd.find($("[name=project_mode]")).val() != 0){
				dcIds += ","+ $formAdd.find($("[name=project_mode]")).val();
			}
			$.post(serverURL + "file/addDatabaseFile", {"tcId" : tcId,"scId" : scId,"dcIds":dcIds,"fileIds":fileIds,"grade":grade,"hierId":hierId}, function(data) {
				if (data.code == 0) {
					   alert("添加成功");
					  cook.addCookie("tcId", tcId);//一级分类
					  cook.addCookie("scId", scId);//二级分类
					  cook.addCookie("detail", $formAdd.find($("[name=detail]")).val());//详情分类1
					  cook.addCookie("content", $formAdd.find($("[name=content]")).val());//详情分类2
					  cook.addCookie("project_mode", $formAdd.find($("[name=project_mode]")).val());//详情分类3
					  
					  cook.addCookie("hierId", hierId);//规划体系
					  cook.addCookie("grade", grade);//等级
					  
					window.location.href="database.htm";
				} else if (data.code != -1) {
					alert("添加失败,请稍后重试");
	           }
			});		
		}
	});
	 /**添加规划体系* */
function addPlan(){
	$.get("view/plan/getList",{"all":true}, function(data) {
		$("#form_add").find("[name=hierId]").empty().html("<option value='0' selected>请选择规划体系...</option>");
		$.each(data.data.list,function(i,k){
		     if( k.hierId == cook.getCookie("hierId")){
					$("#form_add").find("[name=hierId]").append("<option value='" + k.hierId + "' selected>" + k.hierName+ "</option>");
		     }else{
					$("#form_add").find("[name=hierId]").append("<option value='" + k.hierId + "'>" + k.hierName+ "</option>");
		     }
		});
	}, "json");
} 
	/**添加等级***/
function addGrade(){
	if(cook.getCookie("grade") == 1){
		$("#form_add").find("[name=grade]").empty().html("<option value='0' >请选择等级...</option><option value='1'  selected>入门</option><option value='2' >进阶</option>");
	}else if(cook.getCookie("grade") == 2){
		$("#form_add").find("[name=grade]").empty().html("<option value='0' >请选择等级...</option><option value='1' >入门</option><option value='2' selected>进阶</option>");
	}else{
		$("#form_add").find("[name=grade]").empty().html("<option value='0' selected>请选择等级...</option><option value='1' >入门</option><option value='2' >进阶</option>");
	}
 }
});